package com.duoketech.utils.validator.rules.string;

import com.duoketech.exception.ValidateException;
import com.duoketech.utils.validator.AbstractRule;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tianpengtech on 16/8/18.
 */
public class StringMustBeIdentityCard18 extends AbstractRule {
    private String rex = "^[1-9]\\\\d{5}[1-9]\\\\d{3}((0\\\\d)|(1[0-2]))(([0|1|2]\\\\d)|3[0-1])\\\\d{4}$";

    public StringMustBeIdentityCard18(String v, String msg) {
        this.value = v;
        this.msg = msg;
    }

    @Override
    public boolean validate() throws ValidateException {
        String v = StringUtils.trim((String) this.value);
        Pattern pattern = Pattern.compile(this.rex);
        Matcher matcher = pattern.matcher(v);
        if (!matcher.matches()) {
            throw new ValidateException(this.msg);
        }
        return true;
    }
}
